<?php
header('Content-Type: text/html; charset=utf-8');
include('../../includes/classes/core.php');
include('../../includes/classes/barcode/tfpdf.php');
include('../../includes/classes/logger.class.php');

$object_id = $_REQUEST['id'];
$status	   = $_REQUEST['status'];

$res = mysql_query("SELECT 		production.`name` AS `name`,
								CONCAT(sale_transaction_detail.product_quantity, ' ', production_unit.`name`) AS `quantity`
					FROM   		sale_transaction
					LEFT JOIN 	sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id
					RIGHT JOIN 	production ON production.id = sale_transaction_detail.production_id
					LEFT JOIN   production_unit ON production.unit = production_unit.id
					WHERE 		sale_transaction.object_id = $object_id AND sale_transaction.`status` = $status AND sale_transaction_detail.production_id NOT IN (SELECT recipie.recipie_id FROM recipie)");

$res1 = mysql_query("SELECT 			sale_transaction_detail.id,
										pr1.`name` AS `name`,
										CONCAT(sale_transaction_detail.product_quantity, ' ', pu1.`name`) AS `quantity`,
										pr2.`name`AS `ingredient`,
										recipie_producing.quantity / recipie.quantity AS `ing_quantity`
						FROM   			sale_transaction
						LEFT JOIN 		sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id
						LEFT JOIN    	recipie_producing ON recipie_producing.recipie_id = sale_transaction_detail.production_id AND recipie_producing.transaction_detail_id = sale_transaction_detail.id
						LEFT JOIN    	recipie ON recipie_producing.recipie_id = recipie.recipie_id AND recipie_producing.production_id = recipie.ingredient_id
						LEFT JOIN		production AS pr2 ON recipie_producing.production_id = pr2.id
						LEFT  JOIN 		production AS pr1 ON pr1.id = sale_transaction_detail.production_id
						LEFT JOIN    	production_unit  AS pu1 ON pr1.unit = pu1.id
						LEFT JOIN    	production_unit  AS pu2 ON pr2.unit = pu2.id
						WHERE 			sale_transaction.object_id = $object_id AND sale_transaction.`status` = $status AND recipie_producing.quantity > 0 AND `recipie`.`visible` = 1 ");

$res3 = mysql_query("SELECT 		production.`name`,
									CONCAT(sale_transaction_detail.product_quantity, ' ', production_unit.`name`) AS `quantity`       
					FROM   			sale_transaction
					LEFT JOIN	 	sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id
					RIGHT JOIN	 	production ON production.id = sale_transaction_detail.production_id
					LEFT JOIN  		production_unit ON production.unit = production_unit.id
					LEFT JOIN   	recipie ON recipie.recipie_id = production.id
					WHERE 			sale_transaction.object_id = 136 AND sale_transaction.`status` = 4 AND sale_transaction_detail.production_id 
					GROUP BY recipie.recipie_id
					HAVING  sum(recipie.visible)=0
						");



echo '<p align=center style=" font-size: 15px;"><u>'.date('Y-m-d H:i').'</u></p>';
echo '<p align=center style=" font-size: 15px;">ობიექტი: <u>'.getObject($object_id).'</u></p></br>';

echo '<table  style="width: 100%; "  cellspacing="0" cellpadding="6">';

while ($row = mysql_fetch_assoc($res)) {
	echo  	'<tr>';
	echo 	'<td style=" font-size: 14px; width: 35%; border-bottom:1px solid black;"><p>&nbsp&nbsp&nbsp'.$row['name'].'</p></td>';
	echo 	'<td style=" font-size: 14px; width: 15%; border-bottom:1px solid black;"><p>'.$row['quantity'].'</p></td>';
}


$array = array();
while ($row1 = mysql_fetch_assoc($res1)) {
	
	echo  	'<tr>';
	if (in_array($row1[id], $array)){
		
		echo 	'<td style=" font-size: 12px; width: 80%; border-bottom:1px solid black;"><p>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp'.$row1['ingredient'].'</p></td>';
		echo 	'<td style=" font-size: 12px; width: 15%; border-bottom:1px solid black;"><p>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp'.round($row1['ing_quantity']/$row1['quantity'],0).'</p></td>';
		
	}else {
		echo  	'</tr>';
		echo  	'<tr>';
			echo 	'<td style=" font-size: 14px; width: 80%; border-bottom:1px solid black;"><b>&nbsp&nbsp&nbsp'.$row1['name'].'</b></td>';
			echo 	'<td style=" font-size: 14px; width: 15%; border-bottom:1px solid black;"><p>&nbsp'.$row1['quantity'].'</p></td>';
		echo  	'</tr>';
		echo  	'<tr>';
			echo 	'<td style=" font-size: 12px; width: 80%; border-bottom:1px solid black;"><p>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp'.$row1['ingredient'].'</p></td>';
			echo 	'<td style=" font-size: 12px; width: 15%; border-bottom:1px solid black;"><p>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp'.$row1['ing_quantity']/$row1['quantity'].'</p></td>';
	}
	echo  	'</tr>';
	
	$array[] = $row1[id];
}

while($row3 = mysql_fetch_assoc($res3)){
	echo  	'<tr>';
	echo 	'<td style=" font-size: 14px; width: 35%; border-bottom:1px solid black;"><p>&nbsp&nbsp&nbsp'.$row3['name'].'</p></td>';
	echo 	'<td style=" font-size: 14px; width: 15%; border-bottom:1px solid black;"><p>'.$row3['quantity'].'</p></td>';
}

echo '</tr></table>'; 

function getObject($object_id){
	$res = mysql_query("
			SELECT object.name
			FROM   object
			WHERE object.id = '$object_id'");
	
	return mysql_result($res, 0, 'name');
}
	
?>